﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CalemShop
{
    public partial class FrmReportChart : Form
    {
        private bool loadingOk = false;
        private string chartScriptPath;
        public FrmReportChart()
        {
            InitializeComponent();
            string import = @"<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'></script>";
            string body = @"<script src='http://code.highcharts.com/highcharts.js'></script><script src='http://code.highcharts.com/modules/exporting.js'></script><div id='container' style='min-width: 400px; height: 400px; margin: 0 auto'></div>";
            string content = string.Format("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>{0}<script type='text/javascript'>//@REP</script></head><body>{1}</body></html>", import, body);
            webBrowser.DocumentText = content;
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
        }

        void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            loadingOk = true;
        }

        private void btTest_Click(object sender, EventArgs e)
        {
            if (loadingOk)
            {
                StreamReader reader = new StreamReader(doGetChartScriptPath("basicline.js"), Encoding.UTF8);
                string script = reader.ReadToEnd();
                reader.Close();
                string content = webBrowser.DocumentText.Replace("//@REP", script);
                webBrowser.DocumentText = content;
            }

        }


        public static string doGetChartScriptPath(string name)
        {
            string scriptPath = Application.ExecutablePath;
            scriptPath = Path.GetDirectoryName(scriptPath);
            scriptPath = Path.Combine(scriptPath, "data", "js");
            DirectoryInfo jsDir = new DirectoryInfo(scriptPath);
            if (!Directory.Exists(scriptPath))
            {
                jsDir = Directory.CreateDirectory(scriptPath);
            }

            return Path.Combine(scriptPath, name);

        }

        private void btMonthlyIncome_Click(object sender, EventArgs e)
        {
            if (loadingOk)
            {
                StreamReader reader = new StreamReader(doGetChartScriptPath("monthlyIncome.js"), Encoding.UTF8);
                string script = reader.ReadToEnd();
                reader.Close();
                string content = webBrowser.DocumentText.Replace("//@REP", script);
                webBrowser.DocumentText = content;
            }
        }
    }
}
